package com.example.chat.common.utils;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

@Data
@AllArgsConstructor
@Builder
public class ResultUtil<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = -5312741399779950593L;

    private static final Integer SUCCESS = 200;
    private static final String  SUCCESS_MSG = "success";

    private Integer code;
    private String message;
    private T data;
    private Object requestId;
    private boolean success = false;

    public ResultUtil() {

    }

    public static <T> ResultUtil<T> ok(Integer code, T t){
        ResultUtil<T> result = new ResultUtil<>();
        result.setSuccess(true);
        result.setData(t);
        result.setMessage(SUCCESS_MSG);
        result.setCode(code);
        return result;
    }

    public static <T> ResultUtil<T> ok(T t){
        ResultUtil<T> result = new ResultUtil<>();
        result.setSuccess(true);
        result.setData(t);
        result.setMessage(SUCCESS_MSG);
        result.setCode(SUCCESS);
        return result;
    }

    public static <T> ResultUtil<T> ok(){
        ResultUtil<T> result = new ResultUtil<>();
        result.setSuccess(true);
        result.setData(null);
        result.setMessage(SUCCESS_MSG);
        result.setCode(SUCCESS);
        return result;
    }

    public static <T> ResultUtil<T> okWithNullData(){
        ResultUtil<T> result = new ResultUtil<>();
        result.setSuccess(true);
        result.setData(null);
        result.setMessage(SUCCESS_MSG);
        result.setCode(SUCCESS);
        return result;
    }

    public static <T> ResultUtil<T> fail(Integer code, String message){
        ResultUtil<T> result = new ResultUtil<>();
        result.setSuccess(false);
        result.setMessage(message);
        result.setCode(code);
        return result;
    }

    public ResultUtil(boolean success){
        this.success = success;
    }

    public ResultUtil(Integer code, T data){
        this(code, data, null);
    }

    public ResultUtil(Integer code, T data, String message){
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public boolean isSuccess(){
        return success;
    }

}
